const app = getApp();
Page({
  data: {
    addressList: [],
    isShowForm: false,
    formData: {
      id: null,
      fullname: '',
      userPhone: '',
      address: '',
      issDefault:''
    }
  },
  onLoad(options) {

    console.log("onLoad") 
    // 模拟从本地存储获取地址列表
    app.wxRequest('GET', '/userAddress/getList',null, (res) => { 
      if(200==res.code){ 
        this.setData({ 
          addressList:res.data
        }) 
      } 
    }, (err) => { 
      console.log(err.errMsg) 
    }) 
  },
  addAddress() {
    this.setData({
      isShowForm: true,
      formData: {
        id: null,
        fullname: this.data.formData.fullname,
        userPhone: this.data.formData.userPhone,
        address: this.data.formData.address,
        issDefault:this.data.formData.issDefault,
      }
    });
   
  },
  editAddress(e) {
    const index = e.currentTarget.dataset.index;
    const address = this.data.addressList[index];
    this.setData({
      isShowForm: true,
      formData: {
        id: address.id,
        fullname: address.fullname,
        userPhone: address.userPhone,
        address: address.address,
        issDefault:address.isDefault
      }
    });
  },
  deleteAddress(e) {
    const index = e.currentTarget.dataset.index;
    const addressList = this.data.addressList;
    addressList.splice(index, 1);
    this.setData({
      addressList
    });
    wx.setStorageSync('addressList', addressList);
  },
  handleInputChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    const formData = this.data.formData;
    formData[field] = value;
    this.setData({
      formData
    });
  },
  saveAddress() {
    const { formData, addressList } = this.data;
    if (formData.id) {
      // 修改地址
      const index = addressList.findIndex(item => item.id === formData.id);
      addressList[index] = formData;
    } else {
      // 新增地址
     // formData.id = new Date().getTime();
      addressList.push(formData);
    }
    this.setData({
      addressList,
      isShowForm: false
    });
    console.log("null"+this.data.formData) 
    this.saveOrUpdateAddress(this.data.formData)
    /*app.wxRequest('PUT', '/userAddress/saveOrUpdate',this.data.formData, (res) => { 
      if(200==res.code){ 
        app.showMsg("添加成功");
      } else{
        app.showMsg("添加失败");
        return ;
      }
    }, (err) => { 
      console.log(err.errMsg) 
    }) */
    
   // wx.setStorageSync('addressList', addressList);
  },
  cancelForm() {
    this.setData({
      isShowForm: false
    });
  },
  setDefaultAddress(e) {
    const index = e.currentTarget.dataset.index;
    let addressList = this.data.addressList;
    addressList.forEach((item, i) => {
      item.isDefault = i === index;
    });
    this.setData({
      addressList
    });
    console.log(addressList[index]);
    this.saveOrUpdateAddress(addressList[index])
   // wx.setStorageSync('addressList', addressList);
  },
  saveOrUpdateAddress(formData){
    app.wxRequest('PUT', '/userAddress/saveOrUpdate',formData, (res) => { 
      if(200==res.code){ 
        app.showMsg("添加成功");
      } else{
        app.showMsg("添加失败");
        return ;
      }
    }, (err) => { 
      console.log(err.errMsg) 
    }) 
  }
});    